package com.wc.算法提高课.B第二章_搜索.双端队列广搜.电路维修;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/9/24 13:25
 * @description https://www.acwing.com/problem/content/177/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 510, M = N * N, INF = 0x3f3f3f3f;
    static char[][] g = new char[N][];
    static int[][] dist = new int[N][N];
    static boolean[][] st = new boolean[N][N];
    static Deque<Integer> q = new LinkedList<>();
    static int n, m;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            m = sc.nextInt();
            for (int i = 0; i < n; i++) g[i] = sc.next().toCharArray();
            if ((n + m & 1) == 1) out.println("NO SOLUTION");
            else {
                bfs();
                out.println(dist[n][m]);
            }
        }
        out.flush();
    }

    static void bfs() {
        for (int i = 0; i <= n; i++) Arrays.fill(dist[i], INF);
        for (int i = 0; i <= n; i++) Arrays.fill(st[i], false);
        int[] dx = {1, -1, -1, 1}, dy = {1, 1, -1, -1};
        int[] ix = {0, -1, -1, 0}, iy = {0, 0, -1, -1};
        char[] cs = {'\\', '/', '\\', '/'};
        dist[0][0] = 0;

        q.add(0);
        while (!q.isEmpty()) {
            int t = q.pollFirst();
            int x = t / N, y = t % N;

            if (st[x][y]) continue;
            st[x][y] = true;

            for (int i = 0; i < 4; i++) {
                int a = x + dx[i], b = y + dy[i];
                if (a < 0 || b < 0 || a > n || b > m) continue;
                int ga = x + ix[i], gb = y + iy[i];
                int d = dist[x][y] + (g[ga][gb] == cs[i] ? 0 : 1);
                if (d < dist[a][b]) {
                    dist[a][b] = d;
                    if (g[ga][gb] == cs[i]) {
                        q.addFirst(a * N + b);
                    } else q.addLast(a * N + b);
                }
            }
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}